﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace TextTools
{
    /// <summary>
    /// FileFind.xaml 的交互逻辑
    /// </summary>
    public partial class FileFind : Window
    {
        public FileFind()
        {
            InitializeComponent();
        }
        private string findpath = "";
        private string findname = "";
        private string findkind = "";
        private string findcontent = "";
        private float findmin = 0f;
        private float findmax = 0f;

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {// 获取查找目录
            findpath = FindFilePath.Text;
            System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
            if (!string.IsNullOrWhiteSpace(findpath)) fbd.SelectedPath = findpath;
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                findpath = fbd.SelectedPath;
                FindFilePath.Text = findpath;
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {// 开始查找
            findname=FindFileName.Text;
            findkind=FindFileKind.Text;
            findcontent=FindFileContent.Text;
            findpath = FindFilePath.Text;
            findmin=float.Parse(FindFileMin.Text);
            findmax=float.Parse(FindFileMax.Text);
            if (string.IsNullOrWhiteSpace(findpath))
            {
                ShowInfo.Text = "还没有设置查找路径，无法进行查找操作！";
                return;
            }
            ShowInfo.Text = "start find file...";
            FileListView.Items.Clear();
            MyFindFile(findpath,0);
            ShowInfo.Text = "find file is end...";
        }
        private void OnListItemDoubleClick(object sender, MouseButtonEventArgs e)
        {
            int selnum = FileListView.SelectedIndex;
            if (selnum == -1) return;
            CFileView tcfv = new CFileView(FileListView.Items[selnum].ToString());
            tcfv.Owner = this;
            tcfv.Show();
        }
        private bool MyFindFile(string tpath, int level = 0)
        {
            bool result = true;
            System.IO.DirectoryInfo tdi = new System.IO.DirectoryInfo(tpath);
            System.IO.FileInfo[] tfis = tdi.GetFiles(findkind, System.IO.SearchOption.AllDirectories);
            bool isadd = true;
            foreach (System.IO.FileInfo tfi2 in tfis)
            {
                isadd = true;
                if (!string.IsNullOrWhiteSpace(findname))
                {
                    if (tfi2.Name.IndexOf(findname) <0) isadd = false;
                }
                if (isadd && findmax > 0f)
                {
                    if (tfi2.Length < (findmin * 1024 * 1024) || tfi2.Length > (findmax * 1024 * 1024))
                        isadd = false;
                }
                if (isadd && !string.IsNullOrWhiteSpace(findcontent))
                {
                    if (System.IO.File.ReadAllText(tfi2.FullName, EncodingType.GetType(tfi2.FullName)).IndexOf(findcontent) < 0)
                        isadd = false;
                }
                if (isadd) FileListView.Items.Add(tfi2.FullName);
                Dispatcher.Invoke(new Action(() =>
                {
                    ShowInfo.Text = tfi2.Name + " is finish...";
                }), System.Windows.Threading.DispatcherPriority.Render);
            }

            return result;
        }
    }
}
